home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_487 / pprint / source / action.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  17KB  |  667 lines

  1. /************************************************************************
  2. *                                                                                                *
  3. *        action                                                                                *
  4. *                                                                                                *
  5. *-----------------------------------------------------------------------*
  6. *                                                                                                *
  7. *        Module containing the actions, you can do ...                            *
  8. *                                                                                                *
  9. ************************************************************************/
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <intuition/intuition.h>
  14. #include <libraries/dos.h>
  15. #include <libraries/dosextens.h>
  16. #include <libraries/reqbase.h>
  17. #include <private/help.h>
  18.  
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <proto/intuition.h>
  22. #include <proto/graphics.h>
  23. #include <proto/reqproto.h>
  24.  
  25. #include    "settings.h"
  26.  
  27. /***************************************
  28.  
  29.                     Definitions
  30.  
  31. ***************************************/
  32.  
  33. #define    RP            ( FirstWindow->RPort )
  34. #define    TOSIZE    (( SHORT )( 10 * sizeof( struct Settings )))
  35.  
  36. /***************************************
  37.  
  38.                 External References
  39.  
  40. ***************************************/
  41. /***************************************
  42.                     Functions
  43. ***************************************/
  44.  
  45. IMPORT    VOID            RefreshTSize( VOID );
  46. IMPORT    VOID            RefreshRMarg( VOID );
  47. IMPORT    VOID            RefreshLMarg( VOID );
  48. IMPORT    VOID            RefreshPLength( VOID );
  49. IMPORT    VOID            RefreshPWidth( VOID );
  50. IMPORT    VOID            DoRefresh( VOID );
  51.  
  52. IMPORT    VOID            ShowText( SHORT );
  53.  
  54. /***************************************
  55.                     Variables
  56. ***************************************/
  57.  
  58. IMPORT    struct Window                  *FirstWindow;
  59.  
  60. IMPORT    SHORT                                CurLay;
  61. IMPORT    USHORT                            OTSize;
  62. IMPORT    SHORT                                Topline;
  63. IMPORT    UBYTE                                LoadString[];
  64. IMPORT    struct FileInfoBlock          *FIB;
  65. IMPORT    UBYTE                                Dir[],
  66.                                                 File[],
  67.                                                 Path[];
  68. IMPORT    struct FileRequester            FReq;
  69.  
  70. IMPORT    LONG                                Memsize;
  71. IMPORT    UBYTE                              *ActFile;
  72.  
  73. IMPORT    struct Settings                Sets[ 10 ];
  74. IMPORT    SHORT                                ActSet;
  75. IMPORT    UBYTE                                ScTitle[];
  76.  
  77. IMPORT    struct Gadget        TSize,
  78.                                     RMarg,
  79.                                     LMarg,
  80.                                     PLength,
  81.                                     PWidth;
  82.  
  83. /***************************************
  84.  
  85.                     Declarations
  86.  
  87. ***************************************/
  88.  
  89. VOID            About( VOID );
  90. VOID            Load( VOID );
  91. VOID            AskTSize( VOID );
  92. VOID            AskRMarg( VOID );
  93. VOID            AskLMarg( VOID );
  94. VOID            AskPLength( VOID );
  95. VOID            AskPWidth( VOID );
  96. VOID            LoadDefSets( VOID );
  97. VOID            SaveSettings( VOID );
  98. VOID            LoadSettings( VOID );
  99.  
  100. /************************************************************************
  101. *                                                                                                *
  102. *        About                                                                                    *
  103. *                                                                                                *
  104. *-----------------------------------------------------------------------*
  105. *                                                                                                *
  106. *        Well, just try ...                                                                *
  107. *                                                                                                *
  108. *-----------------------------------------------------------------------*
  109. *                                                                                                *
  110. *        Parameters        :                                                                    *
  111. *            none                                                                                *
  112. *                                                                                                *
  113. *        Returns            :                                                                    *
  114. *            none                                                                                *
  115. *                                                                                                *
  116. ************************************************************************/
  117.  
  118. VOID        About( VOID )
  119. {
  120.     SimpleRequest(
  121.     "                       PPrint V1.10\n\n"
  122.     "             © 1991 by Marc Jackisch\n"
  123.     "                       Im Schaufsfeld 17\n"
  124.     "                       4018 Langenfeld\n"
  125.     "                       GERMANY\n\n"
  126.     "This is just a printing tool, capable  of printing a text\n"
  127.     "just as I like it, i. e. tabs  are converted to any size,\n"
  128.     "the pages can be numbered, the printing style can be set,\n"
  129.     "and at the end of each page it  sends a form feed ( which\n"
  130.     "probably is the most important point,  as I never found a\n"
  131.     "tool which can do this ).\n"
  132.     "  Please refer to the doc-file for more information about\n"
  133.     "copyright and distribution.\n"
  134.     "Hope you like it !\n"
  135.     "                                           Marc"
  136.     );
  137. }
  138.  
  139. /************************************************************************
  140. *                                                                                                *
  141. *        Load                                                                                    *
  142. *                                                                                                *
  143. *-----------------------------------------------------------------------*
  144. *                                                                                                *
  145. *        Load a text file.    ( incudes memory allocation, etc. )                    *
  146. *                                                                                                *
  147. *-----------------------------------------------------------------------*
  148. *                                                                                                *
  149. *        Parameters        :                                                                    *
  150. *            none                                                                                *
  151. *                                                                                                *
  152. *        Returns            :                                                                    *
  153. *            none                                                                                *
  154. *                                                                                                *
  155. ************************************************************************/
  156.  
  157. VOID        Load( VOID )
  158. {
  159.     BPTR                            lock;
  160.     BPTR                            datei;
  161.  
  162.     /***/
  163.  
  164.     if( ActFile )
  165.     {
  166.         free( ActFile );
  167.         ActFile    =    NULL;
  168.     }
  169.  
  170.     FReq.Flags    &=    ~FRQSAVINGM;
  171.     FReq.Flags    |=    FRQLOADINGM;
  172.  
  173.     if( !FileRequester( &FReq ))
  174.     {
  175.         strcpy( &ScTitle[20], "< none >" );
  176.         SetWindowTitles( FirstWindow, NULL, ScTitle );
  177.         Topline    =    1;
  178.         InitText();
  179.         ShowText( 0 );
  180.         return;
  181.     }
  182.     lock    =    Lock( Path, ACCESS_READ );
  183.     if( lock == NULL )
  184.     {
  185.         SimpleRequest( "Could not open file\nobject in use" );
  186.         return;
  187.     }
  188.     if( !Examine( lock, FIB ))
  189.     {
  190.         SimpleRequest( "Could not get information on file" );
  191.         UnLock( lock );
  192.         return;
  193.     }
  194.     Memsize    =    FIB->fib_Size;
  195.     UnLock( lock );
  196.     ActFile    =    ( UBYTE * )malloc( Memsize );
  197.     if( ActFile == NULL )
  198.     {
  199.         SimpleRequest( "Could not allocate memory\nfor the file" );
  200.         return;
  201.     }
  202.     datei    =    Open( Path, MODE_OLDFILE );
  203.     if( datei == NULL )
  204.     {
  205.         SimpleRequest( "Could not open file" );
  206.         return;
  207.     }
  208.     if( Read( datei, ActFile, Memsize ) != Memsize )
  209.     {
  210.         Close( datei );
  211.         SimpleRequest( "Error while reading !" );
  212.         return;
  213.     }
  214.     Close( datei );
  215.     stccpy( &ScTitle[20], File, 50 );
  216.     SetWindowTitles( FirstWindow, NULL, ScTitle );
  217.     Topline    =    1;
  218.     InitText();
  219.     ShowText( 0 );
  220.     return;
  221. }
  222.  
  223. /************************************************************************
  224. *                                                                                                *
  225. *        AskTSize                                                                                *
  226. *                                                                                                *
  227. *-----------------------------------------------------------------------*
  228. *                                                                                                *
  229. *        Opens a requester asking for a new tab - size.                            *
  230. *                                                                                                *
  231. *-----------------------------------------------------------------------*
  232. *                                                                                                *
  233. *        Parameters        :                                                                    *
  234. *            none                                                                                *
  235. *                                                                                                *
  236. *        Returns            :                                                                    *
  237. *            none                                                                                *
  238. *                                                                                                *
  239. ************************************************************************/
  240.  
  241. VOID        AskTSize( VOID )
  242. {
  243.     static struct GetLongStruct    GL    =
  244.     {
  245.         "Tab size",
  246.         0L,0L,12L,0L,NULL,0,0L,0L
  247.     };
  248.  
  249.     /***/
  250.  
  251.     if( CurLay == 1 )
  252.         ActivateGadget( &TSize, FirstWindow, NULL );
  253.     else
  254.     {
  255.         GL.defaultval    =    Sets[ ActSet ].Tabsize;
  256.         if( GetLong( &GL ))
  257.         {
  258.             Sets[ ActSet ].Tabsize    =    GL.result;
  259.             RefreshTSize();
  260.             if( OTSize != Sets[ ActSet ].Tabsize )
  261.             {
  262.                 ShowText( Topline );
  263.                 OTSize    =    Sets[ ActSet ].Tabsize;
  264.             }
  265.         }
  266.     }
  267. }
  268.  
  269. /************************************************************************
  270. *                                                                                                *
  271. *        AskRMarg                                                                                *
  272. *                                                                                                *
  273. *-----------------------------------------------------------------------*
  274. *                                                                                                *
  275. *        Opens a requester asking for a new right margin.                        *
  276. *                                                                                                *
  277. *-----------------------------------------------------------------------*
  278. *                                                                                                *
  279. *        Parameters        :                                                                    *
  280. *            none                                                                                *
  281. *                                                                                                *
  282. *        Returns            :                                                                    *
  283. *            none                                                                                *
  284. *                                                                                                *
  285. ************************************************************************/
  286.  
  287. VOID        AskRMarg( VOID )
  288. {
  289.     static struct GetLongStruct    GL    =
  290.     {
  291.         "Right margin",
  292.         0L,0L,100000L,0L,NULL,0,0L,0L
  293.     };
  294.  
  295.     /***/
  296.  
  297.     if( CurLay == 1 )
  298.         ActivateGadget( &RMarg, FirstWindow, NULL );
  299.     else
  300.     {
  301.         GL.defaultval    =    Sets[ ActSet ].RMarg;
  302.         if( GetLong( &GL ))
  303.         {
  304.             Sets[ ActSet ].RMarg    =    GL.result;
  305.             RefreshRMarg();
  306.         }
  307.     }
  308. }
  309.  
  310. /************************************************************************
  311. *                                                                                                *
  312. *        AskLMarg                                                                                *
  313. *                                                                                                *
  314. *-----------------------------------------------------------------------*
  315. *                                                                                                *
  316. *        Opens a requester asking for a new left margin.                            *
  317. *                                                                                                *
  318. *-----------------------------------------------------------------------*
  319. *                                                                                                *
  320. *        Parameters        :                                                                    *
  321. *            none                                                                                *
  322. *                                                                                                *
  323. *        Returns            :                                                                    *
  324. *            none                                                                                *
  325. *                                                                                                *
  326. ************************************************************************/
  327.  
  328. VOID        AskLMarg( VOID )
  329. {
  330.     static struct GetLongStruct    GL    =
  331.     {
  332.         "Left margin",
  333.         0L,0L,100000L,0L,NULL,0,0L,0L
  334.     };
  335.  
  336.     /***/
  337.  
  338.     if( CurLay == 1 )
  339.         ActivateGadget( &LMarg, FirstWindow, NULL );
  340.     else
  341.     {
  342.         GL.defaultval    =    Sets[ ActSet ].LMarg;
  343.         if( GetLong( &GL ))
  344.         {
  345.             Sets[ ActSet ].LMarg    =    GL.result;
  346.             RefreshLMarg();
  347.         }
  348.     }
  349. }
  350.  
  351. /************************************************************************
  352. *                                                                                                *
  353. *        AskPLength                                                                            *
  354. *                                                                                                *
  355. *-----------------------------------------------------------------------*
  356. *                                                                                                *
  357. *        Opens a requester asking for a new page length.                            *
  358. *                                                                                                *
  359. *-----------------------------------------------------------------------*
  360. *                                                                                                *
  361. *        Parameters        :                                                                    *
  362. *            none                                                                                *
  363. *                                                                                                *
  364. *        Returns            :                                                                    *
  365. *            none                                                                                *
  366. *                                                                                                *
  367. ************************************************************************/
  368.  
  369. VOID        AskPLength( VOID )
  370. {
  371.     static struct GetLongStruct    GL    =
  372.     {
  373.         "Page length",
  374.         0L,0L,100000L,0L,NULL,0,0L,0L
  375.     };
  376.  
  377.     /***/
  378.  
  379.     if( CurLay == 2 )
  380.         ActivateGadget( &PLength, FirstWindow, NULL );
  381.     else
  382.     {
  383.         GL.defaultval    =    Sets[ ActSet ].PageLen;
  384.         if( GetLong( &GL ))
  385.         {
  386.             Sets[ ActSet ].PageLen    =    GL.result;
  387.             RefreshPLength();
  388.         }
  389.     }
  390. }
  391.  
  392. /************************************************************************
  393. *                                                                                                *
  394. *        AskPWidth                                                                            *
  395. *                                                                                                *
  396. *-----------------------------------------------------------------------*
  397. *                                                                                                *
  398. *        Opens a requester asking for a new page width.                            *
  399. *                                                                                                *
  400. *-----------------------------------------------------------------------*
  401. *                                                                                                *
  402. *        Parameters        :                                                                    *
  403. *            none                                                                                *
  404. *                                                                                                *
  405. *        Returns            :                                                                    *
  406. *            none                                                                                *
  407. *                                                                                                *
  408. ************************************************************************/
  409.  
  410. VOID        AskPWidth( VOID )
  411. {
  412.     static struct GetLongStruct    GL    =
  413.     {
  414.         "Page width",
  415.         0L,0L,100000L,0L,NULL,0,0L,0L
  416.     };
  417.  
  418.     /***/
  419.  
  420.     if( CurLay == 2 )
  421.         ActivateGadget( &PWidth, FirstWindow, NULL );
  422.     else
  423.     {
  424.         GL.defaultval    =    Sets[ ActSet ].PageWidth;
  425.         if( GetLong( &GL ))
  426.         {
  427.             Sets[ ActSet ].PageWidth    =    GL.result;
  428.             RefreshPWidth();
  429.         }
  430.     }
  431. }
  432.  
  433. /************************************************************************
  434. *                                                                                                *
  435. *        LoadDefSets                                                                            *
  436. *                                                                                                *
  437. *-----------------------------------------------------------------------*
  438. *                                                                                                *
  439. *        Loads the default settings or, if they do not exist, fills the        *
  440. *        "Sets" vector with the values dedicated by preferences.                *
  441. *                                                                                                *
  442. *-----------------------------------------------------------------------*
  443. *                                                                                                *
  444. *        Parameters        :                                                                    *
  445. *            none                                                                                *
  446. *                                                                                                *
  447. *        Returns            :                                                                    *
  448. *            none                                                                                *
  449. *                                                                                                *
  450. ************************************************************************/
  451.  
  452. VOID        LoadDefSets( VOID )
  453. {
  454.     BPTR                            lock,
  455.                                     datei;
  456.     struct Preferences      *Prefs,
  457.                                     PrefBuf;
  458.     SHORT                            i;
  459.     UBYTE                            Name[128]    =    "S:";
  460.  
  461.     /***/
  462.  
  463.     strcpy( &Name[2], DSETNAME );
  464.     lock    =    Lock( Name, ACCESS_READ );
  465.     if( lock == NULL )
  466.     {
  467.         SimpleRequest( "Could not find the default settings.\n"
  468.                             "I will use your preferences" );
  469.         Prefs    =    GetPrefs( &PrefBuf, sizeof( struct Preferences ));
  470.         for( i = 0; i < 10; i++ )
  471.         {
  472.             Sets[ i ].LMarg        =    Prefs->PrintLeftMargin;
  473.             Sets[ i ].RMarg        =    Prefs->PrintRightMargin;
  474.             Sets[ i ].Tabsize        =    8;
  475.             Sets[ i ].PageLen        =    Prefs->PaperLength;
  476.             Sets[ i ].PageWidth    =    Sets[ i ].RMarg - Sets[ i ].LMarg;
  477.             Sets[ i ].Style        =    ( Prefs->PrintPitch != 0 ) ? STYLE_ELITE : STYLE_PICA;
  478.             Sets[ i ].Numb            =    0;
  479.             Sets[ i ].Cond            =    FALSE;
  480.             Sets[ i ].LetterQ        =    ( Prefs->PrintQuality == 0 ) ? FALSE : TRUE;
  481.             Sets[ i ].LSpace6        =    ( Prefs->PrintSpacing == 0 ) ? TRUE : FALSE;
  482.         }
  483.     }
  484.     else
  485.     {
  486.         UnLock( lock );
  487.         datei    =    Open( Name, MODE_OLDFILE );
  488.         if( datei == NULL )
  489.         {
  490.             SimpleRequest(    "Could not open the default settings.\n"
  491.                                 "Sorry, only zeros for you ..." );
  492.             return;
  493.         }
  494.         if( Read( datei, ( UBYTE * )&Sets[0], TOSIZE ) != TOSIZE )
  495.         {
  496.             SimpleRequest(    "Error while reading the default settings !\n"
  497.                                 "The results may be invaluable; please\n"
  498.                                 "check all values before using them !" );
  499.         }
  500.         Close( datei );
  501.     }
  502. }
  503.  
  504. /************************************************************************
  505. *                                                                                                *
  506. *        SaveSettings                                                                        *
  507. *                                                                                                *
  508. *-----------------------------------------------------------------------*
  509. *                                                                                                *
  510. *        Saves the settings to a file.                                                    *
  511. *                                                                                                *
  512. *-----------------------------------------------------------------------*
  513. *                                                                                                *
  514. *        Parameters        :                                                                    *
  515. *            none                                                                                *
  516. *                                                                                                *
  517. *        Returns            :                                                                    *
  518. *            none                                                                                *
  519. *                                                                                                *
  520. ************************************************************************/
  521.  
  522. VOID        SaveSettings( VOID )
  523. {
  524.     BPTR                lock,
  525.                         datei;
  526.     UBYTE                TSD[ DSIZE + 1 ],
  527.                         TSF[ FCHARS + 1 ];
  528.  
  529.     /***/
  530.  
  531.     FReq.Flags    &=    ~FRQLOADINGM;
  532.     FReq.Flags    |=    FRQSAVINGM;
  533.     strcpy( TSD, Dir );
  534.     strcpy( TSF, File );
  535.     strcpy( Dir, "s:" );
  536.     strcpy( File, DSETNAME );
  537.  
  538.     if( FileRequester( &FReq ))
  539.     {
  540.         lock    =    Lock( Path, ACCESS_READ );
  541.         if( lock != NULL )
  542.         {
  543.             if( !TwoGadRequest(    "File %s exists !\n"
  544.                                         "Want to overwrite it ?", File ))
  545.             {
  546.                 UnLock( lock );
  547.                 strcpy( Dir, TSD );
  548.                 strcpy( File, TSF );
  549.                 return;
  550.             }
  551.             UnLock( lock );
  552.         }
  553.         datei    =    Open( Path, MODE_NEWFILE );
  554.         if( datei == NULL )
  555.         {
  556.             SimpleRequest(    "Could not open file\n"
  557.                                 "%s\n"
  558.                                 "for writing !!!", File );
  559.             strcpy( Dir, TSD );
  560.             strcpy( File, TSF );
  561.             return;
  562.         }
  563.         if( Write( datei, ( UBYTE * )&Sets[0], TOSIZE ) != TOSIZE )
  564.         {
  565.             SimpleRequest(    "Error while writing file\n"
  566.                                 "%s.\n"
  567.                                 "Hope, you did not loose your data", File );
  568.         }
  569.         strcpy( Dir, TSD );
  570.         strcpy( File, TSF );
  571.         Close( datei );
  572.     }
  573.     else
  574.     {
  575.         strcpy( Dir, TSD );
  576.         strcpy( File, TSF );
  577.     }
  578. }
  579.  
  580. /************************************************************************
  581. *                                                                                                *
  582. *        LoadSettings                                                                        *
  583. *                                                                                                *
  584. *-----------------------------------------------------------------------*
  585. *                                                                                                *
  586. *        Loads alternate settings                                                        *
  587. *                                                                                                *
  588. *-----------------------------------------------------------------------*
  589. *                                                                                                *
  590. *        Parameters        :                                                                    *
  591. *            none                                                                                *
  592. *                                                                                                *
  593. *        Returns            :                                                                    *
  594. *            none                                                                                *
  595. *                                                                                                *
  596. ************************************************************************/
  597.  
  598. VOID        LoadSettings( VOID )
  599. {
  600.     BPTR        lock,
  601.                 datei;
  602.     UBYTE        TSD[ DSIZE + 1 ],
  603.                 TSF[ FCHARS + 1 ];
  604.  
  605.     /***/
  606.  
  607.     FReq.Flags    &=    ~FRQSAVINGM;
  608.     FReq.Flags    |=    FRQLOADINGM;
  609.     strcpy( TSD, Dir );
  610.     strcpy( TSF, File );
  611.     strcpy( Dir, "s:" );
  612.     strcpy( File, DSETNAME );
  613.  
  614.     if( FileRequester( &FReq ))
  615.     {
  616.         lock    =    Lock( Path, ACCESS_READ );
  617.         if( lock == NULL )
  618.         {
  619.             SimpleRequest( "Could not open file\nobject in use" );
  620.             strcpy( Dir, TSD );
  621.             strcpy( File, TSF );
  622.             return;
  623.         }
  624.         if( !Examine( lock, FIB ))
  625.         {
  626.             SimpleRequest( "Could not get information on file" );
  627.             UnLock( lock );
  628.             strcpy( Dir, TSD );
  629.             strcpy( File, TSF );
  630.             return;
  631.         }
  632.         if( FIB->fib_Size != TOSIZE )
  633.         {
  634.             SimpleRequest(    "A file of a size %ld\n"
  635.                                 "can never be a settings - file", FIB->fib_Size );
  636.             UnLock( lock );
  637.             strcpy( Dir, TSD );
  638.             strcpy( File, TSF );
  639.             return;
  640.         }
  641.         UnLock( lock );
  642.         datei    =    Open( Path, MODE_OLDFILE );
  643.         if( datei == NULL )
  644.         {
  645.             SimpleRequest(    "Could not open file" );
  646.             strcpy( Dir, TSD );
  647.             strcpy( File, TSF );
  648.             return;
  649.         }
  650.         if( Read( datei, ( UBYTE * )&Sets[0], TOSIZE ) != TOSIZE )
  651.         {
  652.             SimpleRequest(    "Error while reading the settings file !\n"
  653.                                 "The results may be invaluable; please\n"
  654.                                 "check all values before using them !" );
  655.         }
  656.         Close( datei );
  657.         DoRefresh();
  658.         strcpy( Dir, TSD );
  659.         strcpy( File, TSF );
  660.     }
  661.     else
  662.     {
  663.         strcpy( Dir, TSD );
  664.         strcpy( File, TSF );
  665.     }
  666. }
  667.